package org.wmq.organizationmanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.wmq.organizationmanagement.mapper.OrganizationMemberMapper;
import org.wmq.organizationmanagement.model.po.Member;
import org.wmq.organizationmanagement.model.po.OrganizationMember;
import org.wmq.organizationmanagement.model.vo.MemberVO;
import org.wmq.organizationmanagement.service.OrganizationMemberService;
import org.wmq.organizationmanagement.service.OrganizationService;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class OrganizationMemberServiceImpl extends ServiceImpl<OrganizationMemberMapper, OrganizationMember> implements OrganizationMemberService {
    private final OrganizationMemberMapper organizationMemberMapper;
    @Override
    public List<MemberVO> getListByOrganizationId(Long organizationId) {
        List<Member> memberList = this.organizationMemberMapper.selectListByOrganizationId(organizationId);
        if (memberList == null){
            log.error("该组织没有任何成员");
            return null;
        }
        return memberList.stream()
                .map(member -> {
                    MemberVO memberVO = new MemberVO();
                    BeanUtils.copyProperties(member,memberVO);
                    return memberVO;
                }).toList();
    }

    @Override
    public List<MemberVO> getListByOrganizationIdAndMemberName(Long organizationId, String memberName) {
        List<Member> memberList = this.organizationMemberMapper.selectListByOrganizationIdAndMemberName(organizationId, memberName);
        if (memberList == null){
            log.error("该组织没有任何同名成员");
            return null;
        }
        return memberList.stream()
                .map(member -> {
                    MemberVO memberVO = new MemberVO();
                    BeanUtils.copyProperties(member,memberVO);
                    return memberVO;
                }).toList();
    }

    @Override
    public boolean deleteByOrganizationAndMemberId(Long organizationId, Long memberId) {
        return this.lambdaUpdate().eq(OrganizationMember::getOrganizationId, organizationId)
                .eq(OrganizationMember::getMemberId, memberId).remove();
    }

    @Override
    public boolean deleteByOrganizationID(Long organizationId) {
        return this.lambdaUpdate().eq(OrganizationMember::getOrganizationId, organizationId).remove();
    }

    @Override
    public boolean deleteByMemberId(Long memberId) {
        return this.lambdaUpdate().eq(OrganizationMember::getMemberId, memberId).remove();
    }
}
